package com.an.spring.beanlife;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.beans.PropertyDescriptor;

public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter{

    //在实例化bean前调用
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {

        if("car".equals(beanName)){
            System.out.println("实例化bean前调用，InstantiationAwareBeanPostProcessorAdapter。postProcessBeforeInstantiation（）");
        }
        return  null;
    }

    /*
    * 接口方法，在实例化Bean之后调用
    * */
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {

        if("car".equals(beanName)){
            System.out.println("实例化Bean之后调用，InstantiationAwareBeanPostProcessorAdapter。postProcessAfterInstantiation");
        }
        return super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {

        if("car".equals(beanName)){
            System.out.println("在设置属性时调用");
        }

        return  pvs;
    }
}
